package com.yh.action;

import com.opensymphony.xwork2.ModelDriven;
import com.yh.entity.Person;
import com.yh.services.IPersonService;
import com.yh.util.BaseAction;
import lombok.Getter;
import lombok.Setter;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import java.util.List;

@Getter
@Setter
@Controller
@Scope(value = "prototype")
@ParentPackage("struts-default")
public class PersonAction extends BaseAction implements ModelDriven {
    private Person person;
    @Autowired
    @Qualifier("personServiceImpl")
    private IPersonService personService;

    @Override
    public Object getModel() {
        if (person == null) {
            person = new Person();
        }
        return person;
    }

    @Action(value = "show", results = {
    @Result(name = "show",location = "/success.jsp")
    })
    public String show() {
        List<Person> list = personService.getAllPerson();
        getRequest().setAttribute("list", list);
        return "show";
    }
    @Action(value = "login", results = {
            @Result(location = "show",type = "redirect"),
            @Result(name = "input",location = "/login.jsp")
    })
    public String login() {
        Person per = personService.login(person);
        if (per != null) {
            getRequest().setAttribute("per", per);
            return SUCCESS;
        } else {
            return INPUT;
        }
    }
    @Action(value = "add", results = {
            @Result(location = "/ok.jsp"),
            @Result(name = "error",location = "/add.jsp")
    })
    public String add() {
        boolean flag = personService.addPerson(person);
        if (flag) {
            return SUCCESS;
        } else {
            return ERROR;
        }
    }

}
